package com.chengqs.service.Impl;

import com.chengqs.common.UserContext;
import com.chengqs.mapper.UserMovieMapper;
import com.chengqs.pojo.BO.UserMoviePageQueryBO;
import com.chengqs.pojo.DTO.PageQuery;
import com.chengqs.pojo.R;
import com.chengqs.pojo.entity.Movie;
import com.chengqs.pojo.entity.UserMovie;
import com.chengqs.service.UserMovieService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserMovieStarImpl implements UserMovieService {
    @Autowired
    private UserMovieMapper userMovieMapper;
    @Override
    public R addToStar(UserMovie userMovie) {
        Long userId = UserContext.getCurrentId().getId();
        if (userId == null) {
            return R.error("用户未登录");
        }

        userMovie.setUserId(userId);

        if (userMovieMapper.selectCountByUserId(userMovie) > 0) {
            return R.error("已存在");
        }

        userMovieMapper.insert(userMovie);
        return R.success();
    }

    @Override
    public R myStar(UserMoviePageQueryBO pageQuery) {
        Long userId = UserContext.getCurrentId().getId();
        if (userId == null) {
            return R.error("用户未登录");
        }
        pageQuery.setUserId(userId);
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());

        Page<Movie> movies = userMovieMapper.getMyStar(pageQuery);

        HashMap<Object, Object> result = new HashMap<>();
        result.put("total",movies.getTotal());
        result.put("list",movies.getResult());
        return R.success(result);
    }
}
